machine:
  timezone:
    Europe/Vienna
  environment:
    ZEMU_: "appemu23"
    EMU_: "appemu21"
    XEMU_: "appemu10"
    Zsdpath: "/sdcard"
    sdpath: "/storage/sdcard"
    Xsdpath: "/mnt/sdcard"
    ANDROID_HOME: /usr/local/android-sdk-linux
    ANDROID_SDK: /usr/local/android-sdk-linux/
    _SDK_: /usr/local/android-sdk-linux/
    GRADLE_OPTS: '-Dorg.gradle.jvmargs="-Xmx1000m -XX:+HeapDumpOnOutOfMemoryError"'
    GRADLE_HOME: ~/g/gradle-2.10
    PATH: ~/g/gradle-2.10/bin:$PATH
    CLASS_P: net.programmierecke.radiodroid2
    START_INTENT_P: .ActivityMain
dependencies:
  cache_directories:
    - ~/.android
    - ~/.gradle
  override:
    - android list sdk --all | grep -i tools
    - android list sdk --all --extended
  pre:
    - sudo apt-get update > /dev/null 2> /dev/null
    - sudo apt-get install xvfb > /dev/null 2> /dev/null
    - sudo apt-get install xdotool > /dev/null 2> /dev/null
    - sudo apt-get install telnet > /dev/null 2> /dev/null
    - sudo apt-get install x11-utils > /dev/null 2> /dev/null
    - sudo apt-get install xvkbd > /dev/null 2> /dev/null
    - sudo apt-get install qrencode > /dev/null 2> /dev/null

    - gradle -v
    - cd ~ ; wget https://services.gradle.org/distributions/gradle-2.10-bin.zip
    - mkdir ~/g ; cd ~/g ; unzip ~/gradle-2.10-bin.zip
    - gradle -v

    - echo y | android update sdk --no-ui --all --filter "tools"
    - echo y | android update sdk --no-ui --all --filter build-tools-23.0.0 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter build-tools-23.0.1 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter build-tools-23.0.2 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter build-tools-23.0.3 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter android-23 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter platform-tools > /dev/null 2> /dev/null

    - echo y | android update sdk --no-ui --all --filter extra-android-m2repository > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter extra-google-m2repository > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter extra-android-support > /dev/null 2> /dev/null

    - echo y | android update sdk --no-ui --all --filter platform-tools-preview > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter sys-img-armeabi-v7a-android-23 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter sys-img-armeabi-v7a-addon-google_apis-google-23 > /dev/null 2> /dev/null
    - echo y | android update sdk --no-ui --all --filter addon-google_apis-google-23 > /dev/null 2> /dev/null

    - pwd ; ls -al

    - git submodule init
    - git submodule update
    - ./gradlew :app:lint --stacktrace --info ; exit 0 # to get lint output
    - cp -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results.html $CIRCLE_ARTIFACTS/ ;  exit 0
    - cp -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results.xml $CIRCLE_ARTIFACTS/ ;  exit 0
    - cp -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results-debug.html $CIRCLE_ARTIFACTS/ ;  exit 0
    - cp -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results-debug.xml $CIRCLE_ARTIFACTS/ ;  exit 0
    - cat -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results.html $CIRCLE_ARTIFACTS/ ;  exit 0
    - cat -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results.xml $CIRCLE_ARTIFACTS/ ;  exit 0
    - cat -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results-debug.html $CIRCLE_ARTIFACTS/ ;  exit 0
    - cat -v /home/ubuntu/RadioDroid/app/build/outputs/lint-results-debug.xml $CIRCLE_ARTIFACTS/ ;  exit 0
    - ./gradlew build --stacktrace --info # normal build

    - find . -name '*.apk' ; exit 0

    - mkdir -p app/build/outputs/apk/
    - cp -v `find ./build/outputs/apk/ -name '*.apk' 2> /dev/null | tail -1 2> /dev/null` app/build/outputs/apk/app-debug-unaligned.apk ; exit 0

    - zip -d app/build/outputs/apk/app-debug-unaligned.apk META-INF/\* ; exit 0  # remove signature (only if it's unsigned) !!
    - cp -av app/build/outputs/apk/app-debug-unaligned.apk ~/app.apk ;
      cd ~/ ;
      echo xxxxxxrm -f ~/.android/debug.keystore ;
      ls -al ~/.android/debug.keystore ;
      if [ ! -f ~/.android/debug.keystore ]; then echo "*** generating new signer key ***" ;
      echo "*** generating new signer key ***";
      echo "*** generating new signer key ***";
      keytool -genkey -v -keystore ~/.android/debug.keystore -storepass android -keyalg RSA -keysize 2048 -validity 10000 -alias androiddebugkey -keypass android -dname "CN=Android Debug,O=Android,C=US" ;
      fi ;
      ls -al ~/ ;
      jarsigner -verbose -keystore ~/.android/debug.keystore -storepass android -keypass android -sigalg SHA1withRSA -digestalg SHA1 -sigfile CERT -signedjar app-signed.apk app.apk androiddebugkey ;
      ls -al ~/ ;
      $_SDK_/build-tools/23.0.1/zipalign -v 4 app-signed.apk app-signed-aligned.apk ;
      ls -al ~/ ;
      pwd ;
      ls -al ;
      cp -av app-signed-aligned.apk $CIRCLE_ARTIFACTS/${CIRCLE_PROJECT_REPONAME}_circleci_$CIRCLE_SHA1.apk || exit 1
    - qrencode -o $CIRCLE_ARTIFACTS/QR_apk.png 'https://circle-artifacts.com/gh/'${CIRCLE_PROJECT_USERNAME}'/'${CIRCLE_PROJECT_REPONAME}'/'${CIRCLE_BUILD_NUM}'/artifacts/'${CIRCLE_NODE_INDEX}'/tmp/'`basename $CIRCLE_ARTIFACTS`'/'"${CIRCLE_PROJECT_REPONAME}_circleci_$CIRCLE_SHA1.apk" ; exit 0
    - qrencode -o $CIRCLE_ARTIFACTS/QR_artifacts.png 'https://circle-artifacts.com/gh/'${CIRCLE_PROJECT_USERNAME}'/'${CIRCLE_PROJECT_REPONAME}'/'${CIRCLE_BUILD_NUM}'#artifacts' ; exit 0
test:
  pre:
    - mksdcard -l e 180M sdcard.img
    - echo 'mtools_skip_check=1' > ~/.mtoolsrc
    - android list targets
#
    - if [ "$EMU_" == "appemu23" ]; then echo "no" | android create avd -n appemu23 -f -t android-23 --abi default/armeabi-v7a --skin "WXGA720" ; fi
    - if [ "$EMU_" == "appemu23" ]; then cat ~/.android/avd/appemu23.avd/config.ini ; fi
#
    - if [ "$EMU_" == "appemu21" ]; then echo "no" | android create avd -n appemu21 -f -t android-21 --abi default/armeabi-v7a --skin "WXGA720" ; fi
    - if [ "$EMU_" == "appemu21" ]; then cat ~/.android/avd/appemu21.avd/config.ini ; fi
#
    - if [ "$EMU_" == "appemu10" ]; then echo "no" | android create avd -n appemu10 -f -t android-10 --abi default/armeabi --skin "WQVGA432" ; fi
    - if [ "$EMU_" == "appemu10" ]; then cat ~/.android/avd/appemu10.avd/config.ini ; fi
#
    - echo "$EMU_"
    - echo "$sdpath"
  override:
    - emulator -avd "$EMU_" -sdcard sdcard.img -no-audio:
        background: true
        parallel: true
    - circle-android wait-for-boot

    - sleep 210 # let the emulator settle down first

    - adb shell input keyevent 82
    - sleep 10
    - import -window root $CIRCLE_ARTIFACTS/capture_emulator_running.png

    - fb-adb shell "cat /proc/meminfo" ; exit 0

    - adb shell ls -al / ; exit 0
    - adb shell ls -al /storage/ ; exit 0
    - adb shell ls -al /sdcard/ ; exit 0

    - adb install ~/app-signed-aligned.apk

    - adb logcat -v time > $CIRCLE_ARTIFACTS/adb_out.txt 2>&1 :
        background: true
    - sleep 60 # let the emulator settle down first

    - fb-adb shell am start -n $CLASS_P/$START_INTENT_P
    - sleep 210 # let the app startup

    - died_count=`cat $CIRCLE_ARTIFACTS/adb_out.txt|grep 'Process. '"$CLASS_P"', PID.'|wc -l|tr -d ' '` ; if [ $died_count -gt 0 ]; then echo "app crashed"; exit 1; fi

    - import -window root $CIRCLE_ARTIFACTS/capture_app_running.png # get proof that the app is running
    - adb shell screencap -p | sed 's/\r$//' > $CIRCLE_ARTIFACTS/capture_app_running_2.png

